/**
 * 用户中心收藏
 */
import {addCart, deleteCollectByIds, getCollectList} from "../../../api"
import {Message} from "element-ui"


export default {
    name: "user-collect",
    data() {
        return {
            imgUrl: process.env.VUE_APP_WAREHOUSE_API,
            showDelete: false,//显示批量删除
            chooseAll: false,
            radio: false,
            collectList: [],//收藏列表数据
            deleteData: [],//待删除的ids数据
            deleteDialogVisible: false,
            page: 1,
            size: 10,
            src:require('../../../assets/test02.jpg')
        }
    },
    created() {
        this.searchList()
    },
    methods: {
        //全选按钮change
        chooseAllChange() {
            this.radio = this.chooseAll
        },
        //查询收藏列表
        searchList() {
            let param = {
                pageNum: this.page,
                pageSize: this.size
            }
            getCollectList(param).then(res => {
                if (res.data.code === 200) {
                    let list = res.data.result.list
                    if (list.length == 0) {
                        this.$message({
                            message: "已全部加载",
                            type: 'info'
                        })
                    }
                    this.collectList = this.collectList.concat(list)
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("获取列表失败！")
            })
        },
        //删除单个
        deleteOne(item) {
            this.deleteData = []
            this.deleteData.push(item.id)
            this.deleteDialogVisible = true
        },
        //删除多个
        deleteMore() {
            this.deleteData = []
            //将列表中选中的放到待删数组中
            this.collectList.forEach(item => {
                if (item.radio) {
                    this.deleteData.push(item.id)
                }
            })
            this.deleteDialogVisible = true
        },
        //删除收藏
        deleteByIds() {
            this.deleteDialogVisible = false
            if (this.deleteData.length == 0) {
                Message.error("至少选择一个...")
                return
            }
            //整理ids
            let ids = ''
            this.deleteData.forEach(id => {
                ids += ids == '' ? id : ',' + id
            })
            console.log(ids)
            const loading = this.$loading({
                lock: true,
                text: '删除中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            })
            let param = {
                ids: ids
            }
            deleteCollectByIds(param).then(res => {
                loading.close()
                if (res.data.code === 200) {
                    Message.success("删除成功")
                    //删除后从新查询
                    this.size = this.collectList.length
                    this.page = 1
                    this.collectList = []
                    this.searchList()
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                loading.close()
                // this.$message.error("删除失败！")
            })
        },

        //加载更多
        loadingMore() {
            this.page++
            this.searchList()
        },

        //加入购物车
        handleAddCart(row){
            const loading = this.$loading({
                lock: true,
                text: "正在加入购物车...",
                spinner: "el-icon-loading",
                background: "rgba(0, 0, 0, 0.7)"
            })
            addCart({
                goodId: row.id,
                projectId: row.projectId,
                payCount: 1,
                serviceType:row.beOverdue,
                goodsType: 1
            }).then(res=>{
                if (res.data.code === 200){
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    })
                }else{
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
                loading.close()
            }).catch(err=>{
                loading.close()
                this.$message.error('加入失败')
            })
        },
    }
}
